package com.module.app.base

import android.content.Context
import android.text.TextUtils
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import com.module.app.bean.ListViewBean
import com.module.base.R
import com.module.base.ext.observe
import com.module.base.network.ERROR
import com.module.base.network.exception.ServerException
import  com.module.app.base.popup.BottomPopupView
import com.module.base.base.adapter.BaseNewAdapter
import com.module.base.utils.CommonUtils
import com.module.base.utils.RecycleViewUtil
import com.module.base.utils.ToastUtils

/**
 * Author: yanxuwen
 * Date: 2023/5/17
 * Desc:
 */
abstract class BottomListPopupView<VM : BaseViewModel, DB : ViewDataBinding, A : BaseNewAdapter<out Any>>(
    context: Context
) :
    BottomPopupView<VM, DB>(context) {
    var recyclerview: RecyclerView? = null
    val adapter: A by lazy {
        createAdapter()
    }

    var isShowNoData = false
    var layoutManager: RecyclerView.LayoutManager? = null

    /**
     * 适配器
     */
    protected abstract fun createAdapter(): A

    /**
     * 列表数据加载。
     */
    protected abstract fun onListLoadPage(
        isPull: Boolean, isRefresh: Boolean, page_no: Int, page_size: Int
    )

    /**
     * 默认 LinearLayoutManager
     */
    open fun getManager(): RecyclerView.LayoutManager? {
        if (layoutManager == null) {
            layoutManager = RecycleViewUtil.getLinear(context)
        }
        return layoutManager

    }

    override fun onCreate() {
        super.onCreate()
        recyclerview = findViewById(R.id.recyclerview)
        recyclerview?.layoutManager = getManager()
        if (adapter.recyclerView == null) {
            adapter.bindToRecyclerView(recyclerview)
        }
        //使用adapter的无数据
        adapter.setAdapterNoData(true)
    }

    override fun initListener() {
        super.initListener()
        adapter.setRefreshPageListenner(null, recyclerview) { isPull, isRefresh ->
            onListLoadPage(
                isPull, isRefresh, adapter.page_no, adapter.page_size
            )
        }
        observe(mViewModel.listFlow) {
            setListData(it)
        }
        observe(mViewModel.errorFlow) {
            onListError(it)
        }
    }

    override fun reload() {
        super.reload()
        adapter.autoRefresh()
    }

    override fun refresh() {
        super.refresh()
        adapter.autoRefresh()
    }


    /**
     * 设置列表数据
     */
    open fun setListData(bean: ListViewBean) {
        if (bean.isRefresh) {
            adapter.setNewData(bean.list as List<Nothing>?)
        } else {
            if (bean.list != null) {
                (bean.list as List<Nothing>?)?.let { adapter.addData(it) }
            }
        }
        showSuccess()
        adapter.loadMoreComplete(true)
        if (adapter.itemCount - adapter.loadMoreViewCount == 0 && (bean.list == null || bean.list.isEmpty())) {
            showEmpty()
        } else if (bean.isNoMore) {
            adapter.loadMoreEnd(isShowNoData)
        }
    }

    /**
     * 设置列表异常
     */
    fun onListError(exception: ServerException?) {
        showSuccess()
        adapter.loadMoreComplete(false)
        if (adapter.itemCount - adapter.loadMoreViewCount == 0) {
            showError(exception)
        } else {
            adapter.loadMoreFail()
            ToastUtils.showShort(if (exception == null || TextUtils.isEmpty(exception.msg)) "异常" else exception.msg)
        }
    }

    fun onListComplete() {
        showSuccess()
        adapter.loadMoreComplete(true)
        adapter.loadMoreEnd(isShowNoData)
    }

    fun onListCancel() {
        onListError(
            ServerException(
                CommonUtils.getString(R.string.error_unknow),
                ERROR.CANCEL
            )
        )
    }

}